﻿//-------------------------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2011 , JamSoft TECH, Ltd .
//-------------------------------------------------------------------------------------

using System;
using System.Web;
using System.Web.UI;

// 本系统生成的类库
using DotNet.Business;
using DotNet.Utilities;

/// <remarks>
/// SetPassword
/// 设置密码
/// 
/// 修改纪录
///	版本：1.0 2011.09.09 LiangMingMing  创建代码。 
/// <author>
///     <verison>1.0</verison>
///		<name>LiangMingMing</name>
///		<date>2011.09.09</date>
///		<description>设置密码</description>
/// </author> 
/// </remarks>
public partial class SetPassword : BasePage
{
    // **********************************************************************************************
    // *                                                                                            * 
    // *                             Public Variate    公共变量                                     *
    // *                                                                                            * 
    // **********************************************************************************************

    /// <summary>
    /// 用户主键
    /// </summary>
    private string UserId
    {
        get
        {
            return this.txtID.Value;
        }
        set
        {
            this.txtID.Value = value;
        }
    }
    /// <summary>
    /// 返回地址
    /// </summary>
    public string ReturnPage
    {
        get
        {
            return this.txtReturnPage.Value;
        }
        set
        {
            this.txtReturnPage.Value = value;
        }
    }
    /// <summary>
    /// 设置密码权限
    /// </summary>
    private bool PermissionSetPassword = false;
    /// <summary>
    /// 多语言变量（添加到收藏夹）
    /// </summary>
    public string ToolbarFavorite = string.Empty;
    /// <summary>
    /// 多语言变量（显示隐藏左边菜单）
    /// </summary>
    public string ToolbarShowhideLeftFrame = string.Empty;
    /// <summary>
    /// 多语言变量（显示隐藏TOP页面）
    /// </summary>
    public string ToolbarShowhideTopFrame = string.Empty;
    /// <summary>
    /// 多语言变量（帮助中心）
    /// </summary>
    public string ToolbarHelp = string.Empty;
    /// <summary>
    /// 多语言-按钮（保存）
    /// </summary>
    public string ButtonConfirm = string.Empty;
    /// <summary>
    /// 多语言-按钮（用户信息）
    /// </summary>
    public string ButtonUserInfo = string.Empty;
    /// <summary>
    /// 多语言-按钮（返回）
    /// </summary>
    public string ButtonReturn = string.Empty;  

    // **********************************************************************************************
    // *                                                                                            * 
    // *                             InitializeComponent  初始化                                    * 
    // *                                                                                            * 
    // **********************************************************************************************

    #region private void PageOnLoad() 页面初次加载时的动作
    /// <summary>
    /// 页面初次加载时的动作
    /// </summary>
    private void PageOnLoad()
    {
        // 获取参数
        this.GetParamter();
    }
    #endregion

    // **********************************************************************************************
    // *                                                                                            * 
    // *                                       Method   操作方法                                    * 
    // *                                                                                            * 
    // **********************************************************************************************

    #region private void Localization() 多语言国际化加载
    /// <summary>
    /// 多语言国际化加载
    /// </summary>
    private void Localization()
    {
        // 多语言国际化加载
        ResourceManagerWrapper.Instance.LoadResources(Server.MapPath("../../../../Resource/Localization/"));
        // 获取语言信息
        this.GetLanguageResource(Utilities.GetCurrentLanguage(this.UserInfo));
    }
    #endregion

    #region private void GetLanguageResource(string languages) 获取语言
    /// <summary>
    /// 获取语言
    /// </summary>
    /// <param name="languages">语言类型</param>
    private void GetLanguageResource(string languages)
    {
        /// <以变量形式加载，刷新时会被刷掉所以得一直加载着>
        // 添加到收藏夹
        this.ToolbarFavorite = ResourceManagerWrapper.Instance.Get(languages, "Toolbar.Favorite");
        // 显示隐藏左边菜单
        this.ToolbarShowhideLeftFrame = ResourceManagerWrapper.Instance.Get(languages, "Toolbar.ShowhideLeftFrame");
        // 显示隐藏TOP页面
        this.ToolbarShowhideTopFrame = ResourceManagerWrapper.Instance.Get(languages, "Toolbar.ShowhideTopFrame");
        // 帮助中心
        this.ToolbarHelp = ResourceManagerWrapper.Instance.Get(languages, "Toolbar.Help");
        // 按钮-保存
        this.ButtonConfirm = ResourceManagerWrapper.Instance.Get(languages, "Button.Save");
        // 按钮-用户信息
        this.ButtonUserInfo = ResourceManagerWrapper.Instance.Get(languages, "SetPassword.aspx_UserInfo");
        // 按钮-返回
        this.ButtonReturn = ResourceManagerWrapper.Instance.Get(languages, "Button.Return");
        /// <用的是控件，所以不怕被刷新>
        if (!Page.IsPostBack)
        {
            // 标题
            this.Title = ResourceManagerWrapper.Instance.Get(languages, "SetPassword.aspx");
            // 确认密码：
            this.ltlConfirmPassword.Text = ResourceManagerWrapper.Instance.Get(languages, "SetPassword.aspx_ltlConfirmPassword");
            // 新密码：
            this.ltlNewPassword.Text = ResourceManagerWrapper.Instance.Get(languages, "SetPassword.aspx_ltlNewPassword");
            // 设置密码
            this.ltlSetPassword.Text = ResourceManagerWrapper.Instance.Get(languages, "SetPassword.aspx_ltlSetPassword");
            // 用户管理-设置密码
            this.ltlTittle.Text = ResourceManagerWrapper.Instance.Get(languages, "SetPassword.aspx_ltlTittle");
            // 提示信息：数据已被其他人删除。
            this.hdfDelete.Value = ResourceManagerWrapper.Instance.Get(languages, "MSG50026");
            // 提示信息：密码不允许为空。
            this.hdfMima.Value = ResourceManagerWrapper.Instance.Get(languages, "MSG50027");
            // 提示信息：设置密码成功。
            this.hdfSetPasswordSuccess.Value = ResourceManagerWrapper.Instance.Get(languages, "MSG50028");
            // 提示信息：新密码不能为空。
            this.hdfXinMima.Value = ResourceManagerWrapper.Instance.Get(languages, "MSG50007");
            // 提示信息：新密码跟确认密码不同。
            this.hdfXinMiMaGenQueRenMiMaBuTong.Value = ResourceManagerWrapper.Instance.Get(languages, "MSG50009");
            // 提示信息：确认密码不能为空。
            this.hdfQueRenMiMa.Value = ResourceManagerWrapper.Instance.Get(languages, "MSG50008");
        }
    }
    #endregion

    #region private void GetPermission() 获取权限
    /// <summary>
    /// 获取权限
    /// </summary>
    private void GetPermission()
    {
        // 获取编辑权限
        this.PermissionSetPassword = this.IsAuthorized("UserAdmin.Edit");
        // 没有权限
        if (!this.PermissionSetPassword)
        {
            // 跳转没有权限页面
            Response.Redirect(Utilities.AccessDenyPage);
        }
    }
    #endregion

    #region private void GetParamter() 读取参数
    /// <summary>
    /// 读取参数
    /// </summary>
    private void GetParamter()
    {
        // 获取用户主键
        if (Page.Request["ID"] != null)
        {
            this.UserId = Page.Request["ID"].ToString();
        }
        else
        {
            this.Page.Response.Redirect(Utilities.DefaultPage);
        }
        // 返回页面地址
        if (Page.Request["ReturnPage"] != null)
        {
            this.txtReturnPage.Value = Page.Request["ReturnPage"].ToString();
        }
    }
    #endregion

    #region private void SetAttributes() 绑定客户端的代码
    /// <summary>
    /// 绑定客户端的代码
    /// </summary>
    private void SetAttributes()
    {
        this.btnConfirm.Attributes.Add("OnClick", "return CheckInput(" + BaseSystemInfo.CheckPasswordStrength.ToString().ToLower() + ");");
    }
    #endregion

    #region private bool CheckInput(string paramNewPassword, string paramConfirmPassword) 检查页面上的输入
    /// <summary>
    /// 检查页面上的输入
    /// </summary>
    /// <param name="paramNewPassword">新密码</param>
    /// <param name="paramConfirmPassword">确认密码</param>
    /// <returns>是否成功</returns>
    private bool CheckInput(string newPassword, string confirmPassword)
    {
        string checkInput = string.Empty;
        // 密码验证
        if (!BaseSystemInfo.CheckPasswordStrength)
        {
            if (newPassword.Length == 0)
            {
                checkInput = "<script>alert('" + this.hdfXinMima.Value + "');document.all['txtNewPassword'].focus();</script>";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", checkInput);
                return false;
            }
            if (confirmPassword.Length == 0)
            {
                checkInput = "<script>alert('" + this.hdfQueRenMiMa.Value + "');document.all['txtConfirmPassword'].focus();</script>";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", checkInput);
                return false;
            }
        }
        if (newPassword != confirmPassword)
        {
            checkInput = "<script>alert('" + this.hdfXinMiMaGenQueRenMiMaBuTong.Value + "');document.all['txtNewPassword'].focus();</script>";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", checkInput);
            return false;
        }
        return true;
    }
    #endregion

    #region private void BatchSetPassword() 设置密码
    /// <summary>
    /// 设置密码
    /// </summary>
    private void BatchSetPassword()
    {
        // 返回值
        int returnValue = 0;
        /// 赋值
        // 新密码
        string newPassword = this.txtNewPassword.Text;
        // 确认密码
        string confirmPassword = this.txtConfirmPassword.Text;
        // 密码验证
        if (!this.CheckInput(newPassword, confirmPassword))
        {
            return;
        }
        // 更新数据
        try
        {
            // 打开数据库连接
            this.UserCenterDbHelper.Open();
            BaseUserManager userManager = new BaseUserManager(this.UserCenterDbHelper, this.UserInfo);
            // 返回状态
            string statusCode = string.Empty;
            // 主键组
            string[] ids = this.UserId.Split(',');
            // 设置密码
            returnValue = userManager.BatchSetPassword(ids, newPassword);
            // 返回信息
            statusCode = userManager.ReturnStatusCode;
            string statusMessage = userManager.GetStateMessage(statusCode);
            //string statusMessage = userManager.GetStateMessage(statusCode);
            if (statusCode.Equals(StatusCode.PasswordCanNotBeNull.ToString()))
            {
                // 提示信息
                Page.ClientScript.RegisterStartupScript(this.GetType(), "setPassword", "<script>alert('" + this.hdfMima.Value + "');</script>");
            }
            else if (statusCode.Equals(StatusCode.SetPasswordOK.ToString()))
            {
                if (Utilities.IsPageRedirect)
                {                   
                    // 提示信息并返回列表
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "setPassword", "<script>alert('" + this.hdfSetPasswordSuccess.Value + "');window.location.href='" + this.ReturnPage + "'</script>");
                }
                else
                {
                    // 提示信息
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "setPassword", "<script>alert('" + this.hdfSetPasswordSuccess.Value + "');</script>");
                }

            }
            else if (statusCode.Equals(StatusCode.ErrorDeleted.ToString()))
            {
                // 提示信息
                Page.ClientScript.RegisterStartupScript(this.GetType(), "setPassword", "<script>alert('" + this.hdfDelete.Value + "');</script>");
            }
        }
        catch (Exception ex)
        {
            // 记录异常信息
            this.LogException(ex);
            // 抛出错误
            throw ex;
        }
        finally
        {
            // 关闭数据库连接
            this.UserCenterDbHelper.Close();
        }
    }
    #endregion

    // **********************************************************************************************
    // *                                                                                            * 
    // *                                       EventArgs    事件方法                                * 
    // *                                                                                            * 
    // ********************************************************************************************** 

    #region protected void Page_Load(object sender, EventArgs e) 页面初次加载的动作
    /// <summary>
    /// 页面初次加载的动作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        // 加载多语言
        this.Localization();
        // 获取权限
        this.GetPermission();
        // 是否回发
        if (!Page.IsPostBack)
        {
            // 页面初次加载时的动作
            this.PageOnLoad();
        }
    }
    #endregion

    #region protected void btnConfirm_Click(object sender, EventArgs e) 设置密码
    /// <summary>
    /// 设置密码
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnConfirm_Click(object sender, EventArgs e)
    {
        // 设置密码
        this.BatchSetPassword();
    }
    #endregion

    #region protected void btnUserInfo_Click(object sender, EventArgs e) 用户信息
    /// <summary>
    /// 用户信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnUserInfo_Click(object sender, EventArgs e)
    {
        Page.Response.Redirect("UserInfo.aspx");
    }
    #endregion

    #region protected void btnReturn_Click(object sender, EventArgs e) 返回
    /// <summary>
    /// 返回
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        // 判断是否带加载效果
        if (Utilities.IsLoading)
            // 转向返回页面（带加载效果）
            Page.Response.Redirect(Utilities.LoadingPageUrl + Server.UrlEncode(this.ReturnPage));
        else
            Page.Response.Redirect(this.ReturnPage);
    }
    #endregion
}